package com.loveyukari.service;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import com.loveyukari.douyu.client.DouyuClient;

/**
 *
 * yangshaohua 2017年4月28日下午2:53:50
 **/
@Service
public class ServerService implements CommandLineRunner{
	private Logger log = LoggerFactory.getLogger(ServerService.class);

	private static String[] DOUYU_SERVER_HOST_ARRAY = {"openbarrage.douyutv.com","119.97.145.175","119.97.145.173"};
	// 暂时停用，需要观察流量消耗
	@Autowired
	private NewYukariRoomService newYukariRoomService;

	@Autowired
	private Environment env;

	private static Map<String, DouyuClient> clientMap = new HashMap<String, DouyuClient>();


	public static void main(String[] args) {
		for (int i = 0; i < 10; i++) {
			DouyuClient douyuClient = new DouyuClient(196, host);
			douyuClient.init();
			douyuClient.start();
		}
	}

	public static String host = DOUYU_SERVER_HOST_ARRAY[0];

	public void init() {
		log.info("room 196 listener start " + host);
		DouyuClient client = clientMap.get(host);
		if (client != null){
			client.close();
			clientMap.remove(host);
		}
		client = new DouyuClient(Integer.parseInt(env.getProperty("yukari.room")	), host, newYukariRoomService);
		clientMap.put(host, client);
		boolean flag = client.init();
		if (flag){
			client.start();
		} else {
			changeServer();
		}
	}

	public void sendMsg(Map<String, Object> map){
		Set<String> hosts = clientMap.keySet();
		for (String host :
				hosts) {
			try{
				DouyuClient douyuClient = clientMap.get(host);
				douyuClient.sendMsg(map);
			} catch (IOException e) {
				log.error(e.getMessage(),e);
			}
		}
	}

	@Scheduled(cron="3,33 * * * * *")
	public void autoCheckServer(){
		Set<String> keys = clientMap.keySet();
		for (String key :
				keys) {
			DouyuClient douyuClient = clientMap.get(key);
			if (host.equals(key)){
				if (douyuClient != null && !douyuClient.isReadyFlag()){
					douyuClient.close();
					clientMap.remove(key);
					init();
				}
			} else {
				if (douyuClient != null){
					douyuClient.close();
				}
				clientMap.remove(key);
			}
		}
		if (clientMap.isEmpty()){
			init();
		}
	}
	public String changeServer(){
		for (int i = 0; i < DOUYU_SERVER_HOST_ARRAY.length; i++) {
			if (StringUtils.equals(host, DOUYU_SERVER_HOST_ARRAY[i])){
				DouyuClient douyuClient = clientMap.get(host);
				if (douyuClient != null){
					douyuClient.close();
					clientMap.remove(host);
				}
				if (i+1 == DOUYU_SERVER_HOST_ARRAY.length){
					host = DOUYU_SERVER_HOST_ARRAY[0];
				} else {
					host = DOUYU_SERVER_HOST_ARRAY[i+1];
				}
				init();
				break;
			}
		}
		return host;
	}

	@Override
	public void run(String... strings) throws Exception {
		init();
	}
}
